﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace WebCumpleanios.fla
{
    public partial class Postal : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClearApplicationCache();

            if (!Page.IsPostBack)
            {
                if ((Page.Request.Params["id"] != null))
                {
                    hfID.Value = Utils.Base64Decode(Page.Request.Params["id"]);

                    Usuario usuario = getUser(hfID.Value.Trim());
                    if (usuario != null)
                    {
                        hfES.Value = usuario.NombreImagen;
                        hfEmail.Value = usuario.Email;
                    }
                }
            }
        }

        public Usuario getUser(string id)
        {
            Usuario usuario = null;
            try
            {
                SqlConnection _cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnx1"].ToString());
                SqlCommand _cmd = new SqlCommand();
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Connection = _cnn;
                _cmd.CommandText = "proc_email";
                _cmd.Parameters.Add(new SqlParameter("@id", id));
                _cnn.Open();

                SqlDataReader lector = _cmd.ExecuteReader();
                while (lector.Read())
                {
                    usuario = new Usuario();
                    usuario.NombreImagen = lector[0].ToString();
                    usuario.Email = lector[1].ToString();
                }
                lector.Close();
                _cnn.Close();
            }
            catch (Exception ex)
            {
                Utils.WriteEventLog(ex);
            }
            
            return usuario;
        }

        public void ClearApplicationCache()
        {
            List<string> keys = new List<string>();

            // retrieve application Cache enumerator
            IDictionaryEnumerator enumerator = this.Page.Cache.GetEnumerator();

            // copy all keys that currently exist in Cache
            while (enumerator.MoveNext())
            {
                keys.Add(enumerator.Key.ToString());
            }

            // delete every key from cache
            for (int i = 0; i < keys.Count; i++)
            {
                Cache.Remove(keys[i]);
            }
        }


    }

    public class Usuario
    {
        public string Email { get; set; }
        public string NombreImagen { get; set; }
    }
}